package com.ezjie.ielts.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ezjie.ielts.core.helper.AppDataBaseHelper;
import com.ezjie.ielts.model.QuestionInfo;

public class QuestionInfoDB {
	private static AppDataBaseHelper dbHelper;
	private SQLiteDatabase db;

	public QuestionInfoDB(Context context) {
		dbHelper = AppDataBaseHelper.getInstance(context);
	}

	// 查找问题
	public synchronized ArrayList<QuestionInfo> query(String g_id) {
		db = dbHelper.getReadableDatabase();

		ArrayList<QuestionInfo> lists = new ArrayList<QuestionInfo>();

		String sql = "select * from " + AppDataBaseHelper.QUESTIONINFO + " where g_id=" + g_id;
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			QuestionInfo info = new QuestionInfo();
			info.g_id = cursor.getString(1);
			info.question_id = cursor.getString(2);
			info.question = cursor.getString(3);
			info.answer = cursor.getString(4);
			info.describe = cursor.getString(5);
			info.is_right = cursor.getString(6);
			info.no = cursor.getString(7);
			lists.add(info);
		}
		cursor.close();
		db.close();

		return lists;
	}

	// 问题组查找问题
	public synchronized ArrayList<QuestionInfo> groupQuery(SQLiteDatabase db, String g_id) {

		ArrayList<QuestionInfo> lists = new ArrayList<QuestionInfo>();

		String sql = "select * from " + AppDataBaseHelper.QUESTIONINFO + " where g_id=" + g_id;
		Cursor cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			QuestionInfo info = new QuestionInfo();
			info.g_id = cursor.getString(1);
			info.question_id = cursor.getString(2);
			info.question = cursor.getString(3);
			info.answer = cursor.getString(4);
			info.describe = cursor.getString(5);
			info.is_right = cursor.getString(6);
			info.no = cursor.getString(7);
			lists.add(info);
		}
		cursor.close();

		return lists;
	}

	// 删除问题
	public synchronized void delete() {
		db = dbHelper.getWritableDatabase();

		String sql = "delete from " + AppDataBaseHelper.QUESTIONINFO;
		db.execSQL(sql);
		db.close();
	}

	// 更新问题
	public synchronized void update() {
		db = dbHelper.getWritableDatabase();

		String sql = "update " + AppDataBaseHelper.QUESTIONINFO + " set answer= ? ";
		db.execSQL(sql, new Object[] { 1 });
		db.close();
	}

	/**
	 * 插入问题
	 * 
	 * @param category
	 */
	public synchronized void insert(QuestionInfo questionInfo) {
		db = dbHelper.getWritableDatabase();

		// db.beginTransaction();
		String sql = "insert into " + AppDataBaseHelper.QUESTIONINFO
				+ " (g_id,question_id,question,answer,describe,is_right,no) " + "values(?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[] { questionInfo.g_id, questionInfo.question_id, questionInfo.question,
				questionInfo.answer, questionInfo.describe, questionInfo.is_right, questionInfo.no });
		db.close();
	}

	/**
	 * 更新问题
	 * 
	 */
	public synchronized void replaceInto(List<QuestionInfo> questions) {
		db = dbHelper.getWritableDatabase();

		for (int i = 0; i < questions.size(); i++) {
			String sql = "replace into " + AppDataBaseHelper.QUESTIONINFO
					+ " (g_id,question_id,question,answer,describe,is_right,no) " + "values(?,?,?,?,?,?,?)";
			db.execSQL(sql, new Object[] { questions.get(i).g_id, questions.get(i).question_id,
					questions.get(i).question, questions.get(i).answer, questions.get(i).describe,
					questions.get(i).is_right, questions.get(i).no });
		}

		db.close();
	}

	/**
	 * 问题组更新问题
	 * 
	 */
	public synchronized void groupReplaceInto(List<QuestionInfo> questions, String g_id, SQLiteDatabase db) {
		for (int i = 0; i < questions.size(); i++) {
			String sql = "replace into " + AppDataBaseHelper.QUESTIONINFO
					+ " (g_id,question_id,question,answer,describe,is_right,no) " + "values(?,?,?,?,?,?,?)";
			db.execSQL(
					sql,
					new Object[] { g_id, questions.get(i).question_id, questions.get(i).question,
							questions.get(i).answer, questions.get(i).describe, questions.get(i).is_right,
							questions.get(i).no });
		}
	}
}
